package com.shenda.api.web;

import java.util.Set;
import java.util.concurrent.CompletableFuture;

import org.springframework.beans.factory.annotation.Autowired;

import com.shenda.api.dto.AnalysisResultDTO;
import com.shenda.api.dto.DataDTO;
import com.shenda.api.dto.UploadDTO;
import com.shenda.api.entity.Data;
import com.shenda.api.service.DatasServiceAsync;
import com.shenda.device.analysis.AnalysisQuery;
import com.swak.annotation.FluxReferer;
import com.swak.annotation.PostMapping;
import com.swak.annotation.RestApi;
import com.swak.entity.Parameters;
import com.swak.entity.Result;
import com.swak.utils.Functions;
import com.swak.utils.StringUtils;
import com.swak.vertx.transport.VertxProxy;

import io.vertx.ext.web.FileUpload;
import io.vertx.ext.web.RoutingContext;

/**
 * 数据 -- 管理
 * 
 * @author lifeng
 */
@RestApi(path = "/api/admin/datas", value = "adminDatasApi")
public class DatasApi {

	@FluxReferer
	private DatasServiceAsync datasService;
	@Autowired
	private VertxProxy vertx;

	/**
	 * 初始化加载
	 * 
	 * @param query
	 * @return
	 */
	@PostMapping("/get")
	public CompletableFuture<Result> get(Data query) {
		return datasService.get(query.getId()).thenApply(result -> AnalysisResultDTO.convert(result))
				.thenApply(Functions.resultFunction());
	}

	/**
	 * 删除数据
	 * 
	 * @param query
	 * @return
	 */
	@PostMapping("/delete")
	public CompletableFuture<Result> delete(Data query) {
		return datasService.delete(query.getId()).thenApply(Functions.resultFunction());
	}

	/**
	 * 分页数据管理
	 * 
	 * @return
	 */
	@PostMapping("/page")
	public CompletableFuture<Result> page(Data query, Parameters param) {
		return datasService.page(query, param).thenApply(page -> DataDTO.convertPage(page))
				.thenApply(Functions.resultFunction());
	}

	/**
	 * 图标展示
	 * 
	 * @param query
	 * @return
	 */
	@PostMapping("/chart")
	public CompletableFuture<Result> chart(AnalysisQuery query) {
		return datasService.chart(query).thenApply(result -> AnalysisResultDTO.convert(result))
				.thenApply(Functions.resultFunction());
	}

	/**
	 * 导入文件
	 * 
	 * @return
	 */
	@PostMapping("/upload")
	public CompletableFuture<Result> upload(RoutingContext context) {

		// 结果
		CompletableFuture<Result> resultFuture = new CompletableFuture<Result>();

		// 文件
		Set<FileUpload> files = context.fileUploads();
		if (files.size() == 0) {
			resultFuture.complete(Result.error("文件上传失败！"));
			return resultFuture;
		}

		// 有文件
		final FileUpload file = files.iterator().next();

		// 上传了文件
		if (StringUtils.isNotBlank(file.uploadedFileName())) {
			vertx.fileSystem().readFile(file.uploadedFileName(), res -> {
				String mac = file.fileName().split("-")[0];
				byte[] bytes = res.result().getBytes();
				vertx.fileSystem().deleteBlocking(file.uploadedFileName());
				datasService.upload(new UploadDTO().setDatas(bytes).setMac(mac)).whenComplete((r, e) -> {
					resultFuture.complete(Result.success());
				});
			});
		} else {
			resultFuture.complete(Result.error("文件上传失败！"));
		}
		// 返回结果
		return resultFuture;
	}
}